﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Data.SqlClient;
using System.Web;

namespace ChatBox.Web.UI
{
    public static class LopKetNoi
    {
        private static SqlConnection c = new SqlConnection("Data Source =.; Initial Catalog=ChatBox;Integrated Security=true;");

        private static void MoKetNoi()
        {
            if (c.State != ConnectionState.Open)
            {
                c.Open();
            }
        }

        private static void DongKetNoi()
        {
            if (c.State == ConnectionState.Open)
            {
                c.Close();
            }
        }

        public static DataSet TruyVanDuLieu(string strSQL,string strTenBang)
        {
            DataSet dataSet = new DataSet();

            try
            {
                MoKetNoi();
                SqlDataAdapter adapter = new SqlDataAdapter(strSQL, c);
                adapter.Fill(dataSet,strTenBang);
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
            }
            finally
            {
                DongKetNoi();
            }
            return dataSet;
        }

        public static bool CapNhatDuLieu(string strSQL)
        {
            bool bKQ = true;
            try
            {
                MoKetNoi();
                SqlCommand command = new SqlCommand(strSQL, c);
                command.ExecuteNonQuery();

            }
            catch
            {
                bKQ = false;

            }
            finally
            {
                DongKetNoi();
            }
            return bKQ;
        }
    }
}